package com.ttx.ch04;

import java.util.List;
import java.util.stream.Stream;

import static java.util.stream.Collectors.toList;

public class DishStreamTest {
    static List<Dish> dishs = null;

    public DishStreamTest() {
        dishs = Dish.getDishs();
    }

    public static void main(String[] args) {
        DishStreamTest dishStreamTest = new DishStreamTest();
        test1();
        test2();
    }

    public static void test1() {
        List<String> dishNameList = dishs.stream()
                .filter(dish -> dish.getCalories() > 100)
                .map(Dish::getName)
                .collect(toList());
        System.out.println(dishNameList);
    }

    public static void test2() {
        Stream stream = dishs.stream()
                .filter(dish -> dish.getCalories() < 500)
                .map(Dish::getName);
        stream.forEach(System.out::println);
        // 流只能消费一次，因此下述语句将会报错
        // stream.forEach(System.out::println);

    }
}
